﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Array2DExtensions
{
    /// <summary>
    /// Represents the value in a 2d array
    /// Stanislav Radkov®  2012 :D 
    /// </summary>
    public class Cell<T> : Index, IComparable where T : IComparable<T>
    {
        public T Value { get; set; }

        #region Constructors

        public Cell(T[,] array, Index cellIndex) : this(array[cellIndex.Row, cellIndex.Col], cellIndex)
        {
        }

        public Cell(T value, Index cellIndex) : base(cellIndex.Row, cellIndex.Col)
        {
            this.Value = value;
        }

        public Cell(T value, Int32 row, Int32 col) : base(row, col)
        {
            this.Value = value;
        }

        #endregion

        #region Public Methods

        public int CompareTo(object obj)
        {
            if (null == obj)
                return 1;

            Cell<T> otherCell = obj as Cell<T>;
            if (otherCell != null)
            {
                return this.Value.CompareTo(otherCell.Value);
            }
            else
            {
                throw new IndexOutOfRangeException("Object is not a Cell");
            }
        }

        public override int GetHashCode()
        {
            return this.Value.GetHashCode();
        }
         
        public override string ToString()
        {
            return base.ToString() + string.Format(" Value:{0}", this.Value);
        }
        #endregion
    }
}
